package longrise.phone.com.bjjt_henan.myaccident;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.longrise.android.FormParameter;
import com.longrise.android.LFView;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Adapter.PageAdapter;
import longrise.phone.com.bjjt_henan.R;

/**
 * 我的事故主界面
 */
public class AccidentMainView extends LFView {

    private Context mContext;

    private LinearLayout mView;

    private PageView pageView;

    private AccidentListView dealLView;//待处理列表

    private AccidentListView finishLView;//已处理列表

    public AccidentMainView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public View getView() {
        return mView;
    }

    @Override
    public void init() {
        if (null == mContext) {
            return;
        }
        mView = new LinearLayout(mContext);
        mView.setOrientation(LinearLayout.VERTICAL);
        //标题栏
        View titleView = LayoutInflater.from(mContext).inflate(R.layout.common_titlehn_layout, null);
        TextView titleTV = (TextView) titleView.findViewById(R.id.tv_title);
        titleTV.setText(R.string.my_accident);
        titleView.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeForm();
                OnDestroy();
            }
        });
        LayoutParams titleParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        mView.addView(titleView, titleParams);
        //内容
        if (pageView == null) {
            initPageView();
        }
        LayoutParams pageParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        mView.addView(pageView.getView(), pageParams);

        dealLView.refresh();
    }

    //实例化滑动页
    public void initPageView() {
        List<LFView> lists = new ArrayList<LFView>();
        if (null == dealLView) {
            dealLView = new AccidentListView(mContext, 0);
            dealLView.init();
        }
        lists.add(dealLView);
        if (null == finishLView) {
            finishLView = new AccidentListView(mContext, 1);
            finishLView.init();
        }
        lists.add(finishLView);

        final PageAdapter adapter = new PageAdapter();
        adapter.setLists(lists);

        pageView = new PageView(mContext) {
            @Override
            public String[] getTitles() {
                return new String[]{"处理中", "已完成"};
            }

            @Override
            public PagerAdapter getAdapter() {
                return adapter;
            }

            @Override
            public void itemChange(int position) {
                if (position == 0) {
                    dealLView.refresh();
                } else if (position == 1) {
                    finishLView.refresh();
                }
            }
        };
        pageView.init();
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        mView = null;
        if (null != pageView) {
            pageView.OnDestroy();
        }
        pageView = null;
        if (null != dealLView) {
            dealLView.OnDestroy();
        }
        dealLView = null;
        if (null != finishLView) {
            finishLView.OnDestroy();
        }
        finishLView = null;
    }
}
